package views;

import views.panels.PanelGeneral;
import logicBusiness.EstudianteBL;
import logicBusiness.GradoBL;
import models.Estudiante;
import java.awt.BorderLayout;
import java.util.List;
import java.util.Vector;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;

/**
 *
 * @author vanessa
 */
public class InterfazMatricula extends javax.swing.JFrame {

    EstudianteBL estudiantebo;
    private GradoBL gradobo;

    private void inicializarTablaDeEstudiantes(List<Estudiante> listaentrada) {
        int numfilas = listaentrada.size();

        TEstudiantesMatri.setModel(new DefaultTableModel(
                new Object[numfilas][5],
                new String[]{
                    "Documento", "Apellido", "Nombre", "Fecha de inscripcion", "Grado al que aspira", "Matriculado"
                }));


        for (int indicefilas = 0; indicefilas < numfilas; indicefilas++) {
//            this.TEstudiantesMatri.setValueAt(listaentrada.get(indicefilas).getDocEstudiante(), indicefilas, 0);
//            this.TEstudiantesMatri.setValueAt(listaentrada.get(indicefilas).getApellidoEst(), indicefilas, 1);
//            this.TEstudiantesMatri.setValueAt(listaentrada.get(indicefilas).getNombreEst(), indicefilas, 2);
//            this.TEstudiantesMatri.setValueAt(listaentrada.get(indicefilas).getFechaPreinscripcion(), indicefilas, 3);
//            this.TEstudiantesMatri.setValueAt(listaentrada.get(indicefilas).getIdGrado(), indicefilas, 4);
//            if (listaentrada.get(indicefilas).getMatriculado() == 0) {
//                this.TEstudiantesMatri.setValueAt("No", indicefilas, 5);
//            } else if (listaentrada.get(indicefilas).getMatriculado() == 1) {
//                this.TEstudiantesMatri.setValueAt("Si", indicefilas, 5);
//            }
        }

        gradobo = new GradoBL();
        estudiantebo = new EstudianteBL();
        Vector vecgrados = new Vector(gradobo.consultarGrados());
        this.CBGrado.setModel(new DefaultComboBoxModel(vecgrados));
    }

    /** Creates new form InterfazMatricula */
    public InterfazMatricula() {
        initComponents();
        PanelGeneral panel = new PanelGeneral();
        this.add(panel, BorderLayout.SOUTH);

        TableColumn column = null;

        /*Cambio el ancho de las columnas de la tabla TEstudiantesMatri*/
        column = this.TEstudiantesMatri.getColumnModel().getColumn(0);
        column.setPreferredWidth(85);
        column = this.TEstudiantesMatri.getColumnModel().getColumn(1);
        column.setPreferredWidth(100);
        column = this.TEstudiantesMatri.getColumnModel().getColumn(2);
        column.setPreferredWidth(100);
        column = this.TEstudiantesMatri.getColumnModel().getColumn(3);
        column.setPreferredWidth(150);
        column = this.TEstudiantesMatri.getColumnModel().getColumn(4);
        column.setPreferredWidth(70);
        column = this.TEstudiantesMatri.getColumnModel().getColumn(5);
        column.setPreferredWidth(30);
        estudiantebo = new EstudianteBL();
//        this.inicializarTablaDeEstudiantes(estudiantebo.obtenerListadoInscritos());
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        SPEstudiantesMatri = new javax.swing.JScrollPane();
        TEstudiantesMatri = new javax.swing.JTable();
        CBMatricular = new javax.swing.JButton();
        CBDeshacer = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        BFiltrar = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        CBGrado = new javax.swing.JComboBox();
        BCancelar = new javax.swing.JButton();

        setTitle("Matricular estudiantes");
        setResizable(false);

        jLabel1.setText("Marque con una \"S\" o con la palabra \"si\" los estudiante que formalizaron su matricula:");

        TEstudiantesMatri.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null}
            },
            new String [] {
                "Documento", "Apellido", "Nombre", "Fecha de inscripcion", "Grado al que esta inscrito", "Matriculado"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, true
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        TEstudiantesMatri.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_ALL_COLUMNS);
        SPEstudiantesMatri.setViewportView(TEstudiantesMatri);
        TEstudiantesMatri.getColumnModel().getColumn(0).setPreferredWidth(170);
        TEstudiantesMatri.getColumnModel().getColumn(1).setPreferredWidth(300);
        TEstudiantesMatri.getColumnModel().getColumn(2).setPreferredWidth(80);

        CBMatricular.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        CBMatricular.setText("Matricular estudiantes seleccionados");
        CBMatricular.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CBMatricularActionPerformed(evt);
            }
        });

        CBDeshacer.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        CBDeshacer.setText("Deshacer cambios");
        CBDeshacer.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CBDeshacerActionPerformed(evt);
            }
        });

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "DATOS DE BUSQUEDA", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 11), new java.awt.Color(0, 0, 255))); // NOI18N

        BFiltrar.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        BFiltrar.setText("Filtrar Busqueda");
        BFiltrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BFiltrarActionPerformed(evt);
            }
        });

        jLabel2.setText("Seleccione un grado al que se asignaron los estudiantes:");

        CBGrado.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(CBGrado, javax.swing.GroupLayout.PREFERRED_SIZE, 196, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 168, Short.MAX_VALUE)
                .addComponent(BFiltrar, javax.swing.GroupLayout.PREFERRED_SIZE, 153, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(BFiltrar)
                    .addComponent(jLabel2)
                    .addComponent(CBGrado, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        BCancelar.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        BCancelar.setText("Cancelar");
        BCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BCancelarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(SPEstudiantesMatri, javax.swing.GroupLayout.DEFAULT_SIZE, 828, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jLabel1)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(CBDeshacer, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(CBMatricular, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 414, Short.MAX_VALUE)
                        .addComponent(BCancelar, javax.swing.GroupLayout.PREFERRED_SIZE, 171, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(SPEstudiantesMatri, javax.swing.GroupLayout.PREFERRED_SIZE, 225, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(CBDeshacer)
                .addGap(3, 3, 3)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(CBMatricular)
                    .addComponent(BCancelar))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void CBMatricularActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CBMatricularActionPerformed
        String temp = null;
        for (int i = 0; i < this.TEstudiantesMatri.getRowCount(); i++) {
            this.TEstudiantesMatri.setValueAt(String.valueOf(this.TEstudiantesMatri.getValueAt(i, 5)).toLowerCase(), i, 5); //conversion a minusculas
        }
//        int numatriculados = estudiantebo.actualizarAMatriculados(TEstudiantesMatri);
        
//        JOptionPane.showMessageDialog(this,
//                "Se han inscrito " + numatriculados
//                + " Estudiantes",
//                "Operacion exitosa",
//                JOptionPane.INFORMATION_MESSAGE);
    }//GEN-LAST:event_CBMatricularActionPerformed

    private void CBDeshacerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CBDeshacerActionPerformed
        int opcion = JOptionPane.showConfirmDialog(this,
                "Esta opcion solo revertira cambios de estudiantes no matriculados ¿desea continuar?",
                "Advertencia",
                JOptionPane.YES_NO_OPTION);
        if (opcion == JOptionPane.YES_OPTION) {
//            this.inicializarTablaDeEstudiantes(estudiantebo.obtenerListadoInscritos());
        } else if (opcion == JOptionPane.NO_OPTION);
    }//GEN-LAST:event_CBDeshacerActionPerformed

    private void BFiltrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BFiltrarActionPerformed
        String temp = new String(this.CBGrado.getSelectedItem().toString());
//        if (!estudiantebo.encontrarEstudiantesPorGradoInscritos(temp).isEmpty()) {
//            this.inicializarTablaDeEstudiantes((List<Estudiante>) estudiantebo.encontrarEstudiantesPorGradoInscritos(temp));
//        } else {
//            JOptionPane.showMessageDialog(this,
//                    "No se han encontrado estudiantes matriculados que aspiren al grado:\n\n"
//                    + temp,
//                    "Error",
//                    JOptionPane.OK_OPTION);
//        }

    }//GEN-LAST:event_BFiltrarActionPerformed

    private void BCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BCancelarActionPerformed
        this.setVisible(false);
    }//GEN-LAST:event_BCancelarActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new InterfazMatricula().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton BCancelar;
    private javax.swing.JButton BFiltrar;
    private javax.swing.JButton CBDeshacer;
    private javax.swing.JComboBox CBGrado;
    private javax.swing.JButton CBMatricular;
    private javax.swing.JScrollPane SPEstudiantesMatri;
    private javax.swing.JTable TEstudiantesMatri;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    // End of variables declaration//GEN-END:variables
}
